package com.kidedu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import net.sf.json.JSONObject;

import com.kidedu.service.IKGradeService;
import com.kidedu.util.SpringContextUtil;
import com.opensymphony.xwork2.Action;


public class KGradeBatchImportAction {

    private 	 IKGradeService  kGradeService= (IKGradeService) SpringContextUtil.getBean("iKGradeService");
    private File file;
    private  JSONObject jsonObj=new JSONObject();
    
    public String BatchImport(){
    	 Workbook book = null;
	        try {
	            OPCPackage pkg = OPCPackage.openOrCreate(file);
	            book = new XSSFWorkbook(pkg);
	        } catch (Exception ex) {
	            try {
					book = new XSSFWorkbook(new FileInputStream(file));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
		   try{
		      
			   Sheet sheet=book.getSheetAt(0);  				
			   jsonObj=kGradeService.BatchImport(sheet);
		       
		   }
		   catch(Exception e)
		   {
			   System.out.println(e);
		   }
		
    	return Action.SUCCESS;
    }
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public JSONObject getJsonObj() {
		return jsonObj;
	}
	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}
}
